//
//  NetUrl.m
//  CarHome
//
//  Created by 王建男 on 14-9-17.
//  Copyright (c) 2014年 WJN_work@163.com. All rights reserved.
//

#import "NetUrl.h"
#import "AFNetworking.h"
#import "Reachability.h"

@implementation NetUrl

+ (void )connect:(NSString *)urlStr parmater:(NSDictionary *)dic Block:(void (^)(NSData *data))block
{
    /****************************************************************************
     *1.判断当前网络状态 ，无网络服务不调用网络连接 ， 3G状态提醒用户切换WIFI , WIFI正常通讯
     *2.判断网络获取数据本地是否存在缓存 ，存在缓存从本地提取 ， 不存在网络获取
     ****************************************************************************/
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *path = [paths lastObject];
    NSString *file = [NSString stringWithFormat:@"%@/%lu.data", @"Caches", (unsigned long)[urlStr hash]];
    path = [NSString stringWithFormat:@"%@/%@", path, file];
    

    //第三方网络请求
    if ([self isConnectionAvailable]) {
          AFHTTPRequestOperationManager *mannager = [AFHTTPRequestOperationManager manager];
          mannager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
          [mannager GET:urlStr parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
         //将网络获取数据缓存至本地
        
          NSData *data = [NSJSONSerialization JSONObjectWithData:operation.responseData options:NSJSONReadingMutableContainers error:NULL];
        
          block(data);
          [operation.responseData writeToFile:path atomically:YES];
        
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
      }];
    } else {
        
        //将本地数据读取
        if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
            NSData *data_local = [NSData dataWithContentsOfFile:path];
            id objects = [NSJSONSerialization JSONObjectWithData:data_local options:NSJSONReadingMutableContainers error:NULL];
            data_local = [NSData dataWithData:objects];
            block(data_local);
        }
    }

}

#pragma mark -
#pragma mark 判断网络状态

+(BOOL) isConnectionAvailable{
    
    BOOL isExistenceNetwork = YES;
    Reachability *reach = [Reachability reachabilityWithHostname:@"www.apple.com"];
    switch ([reach currentReachabilityStatus]) {
        case NotReachable:
            isExistenceNetwork = NO;
            NSLog(@"notReachable");
            break;
        case ReachableViaWiFi:
            isExistenceNetwork = YES;
            break;
        case ReachableViaWWAN:
            isExistenceNetwork = YES;
            break;
    }
    return isExistenceNetwork;
}
//是否有网络
+ (BOOL )isExistencenetWork
{
    Reachability *reach = [Reachability reachabilityWithHostname:@"www.apple.com"];
    if ([reach currentReachabilityStatus] == ReachableViaWWAN) {
        return YES;
    } else {
        return NO;
    }
}
//是否有网络
+ (BOOL )isExistencenetNot
{
    Reachability *reach = [Reachability reachabilityWithHostname:@"www.apple.com"];
    if ([reach currentReachabilityStatus] == NotReachable) {
        return NO;
    } else {
        return YES;
    }

}
@end
